﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace AnaViewer
{
    /// <summary>
    /// 对呈现“项的语录”的过程进行封装。
    /// </summary>
    /// <typeparam name="T">此 ItemSayingView 将绘制的项的类型。</typeparam>
    public class ItemSayingView<T> : ViewBase, IDisposable where T : IItem
    {
        private static Brush textDrawingBrush = Brushes.Black;
        private T displayItem;


        public T DisplayItem
        {
            get { return displayItem; }
            set { displayItem = value; this.Invalidate(); }
        }




        Color borderColor = Color.FromArgb(255, Color.White);
        /// <summary>
        /// 描边的颜色。
        /// </summary>
        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }
        int borderWidth = 4;
        public int BorderWidth
        {
            get { return borderWidth; }
            set { borderWidth = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }
        float flatness = 1.0F;
        /// <summary>
        /// 指定曲线展平的值。
        /// </summary>
        public float Flatness
        {
            get { return flatness; }
            set { flatness = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }
        Font timerFont = Control.DefaultFont;
        /// <summary>
        /// 时间的字体
        /// </summary>
        public Font TimeFont
        {
            get { return timerFont; }
            set { timerFont = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }

        public ItemSayingView()
        {
            // 标题（格言句子）的字体
            base.Font = Control.DefaultFont;
        }

        RectangleF realRect = RectangleF.Empty;
        /// <summary>
        /// 真正绘制占用的区域。
        /// </summary>
        public RectangleF RealRect
        {
            get { return realRect; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            string title = "";
            string time = "";
            string author = "";

            if (displayItem == null)
            {
                title = "还没有获得语录数据，您还是再等会吧～";
                time = "";
                author = "范传根";
            }
            else
            {
                title = displayItem.Title;
                time = displayItem.PubDate;
                if (displayItem.Author == "")
                    author = "佚名";
                else
                    author = displayItem.Author;
            }

            Rectangle rect = e.ClipRectangle;

            //格言句子的区域（留 10 像素的边距）
            Rectangle titleRect = new Rectangle(rect.X + 10, rect.Y + 10, rect.Width - 20, rect.Height - 20);

            // 更改图形设置以绘制清晰的文本
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;//ClearTypeGridFit
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.Clear(backColor);

            using (GraphicsPath path = new GraphicsPath(FillMode.Alternate))
            {

                using (StringFormat titleFormat = new StringFormat())
                {
                    titleFormat.Alignment = StringAlignment.Near;
                    titleFormat.LineAlignment = StringAlignment.Near;
                    //格言
                    path.AddString(title, Font.FontFamily, (int)Font.Style, Font.Size, titleRect, titleFormat);
                }
                RectangleF sayingBounds = path.GetBounds();

                if (time != "")
                {//时间有效，则绘制
                    //绘制作者的区域
                    RectangleF authorRect = new RectangleF(titleRect.X, (int)sayingBounds.Bottom + 10, titleRect.Width, titleRect.Height - (int)(sayingBounds.Height + 20));
#if DEBUG_SHOWBORDER
                    g.DrawRectangle(Pens.Red, Rectangle.Truncate(authorRect));
#endif
                    using (StringFormat authorFormat = new StringFormat(StringFormatFlags.LineLimit))
                    {
                        authorFormat.Alignment = StringAlignment.Far;
                        authorFormat.LineAlignment = StringAlignment.Near;
                        authorFormat.Trimming = StringTrimming.EllipsisWord;
                        path.AddString(time, timerFont.FontFamily, (int)timerFont.Style, timerFont.Size, authorRect, authorFormat);
                    }
                }

                if (borderWidth > 0)
                {//描边
                    GraphicsPath pathBorder = path.Clone() as GraphicsPath;
                    pathBorder.Widen(new Pen(borderColor, borderWidth), null, flatness);
                    g.FillPath(new SolidBrush(borderColor), pathBorder);
                    g.DrawPath(new Pen(borderColor, 1), pathBorder);
                }
                using (Brush textBrush = new SolidBrush(this.ForeColor))
                {
                    //g.DrawPath(new Pen(this.ForeColor), path);
                    g.FillPath(textBrush, path);
                }

                RectangleF pathRect =path.GetBounds();
                pathRect.Offset(this.Location);//转换为父坐标
                this.realRect = pathRect;

#if DEBUG_SHOWBORDER
                g.DrawRectangle(Pens.Red,titleRect);
#endif
            }


            base.OnPaint(e);
        }

        /// <summary>
        /// 释放所有不再需要的字段
        /// </summary>
        public override void Dispose()
        {
            //fadeTimer.Dispose();

            base.Dispose();
        }

    }
}
